package com.dongdongshop.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.vo.CartVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@DubboService
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private RedisTemplate redisTemplate;
    @DubboReference
    private ItemService itemService;

    //我们点击加入购物车,共有三种情况,
//    1.商家存在,商品不存在
//    2.商家存在,商品也存在
//    3.商家不存在,并且商品也不存在
    @Override
    public List<CartVo> selectItemById(List<CartVo> cartlist, Long itemId, Integer num) {
        //1.根据商品的id查询商品数据
        TbItem item = itemService.getItemByItemId(itemId);
        //2.根据查出来的商品数据,获取他的商家
        String sellerId = item.getSellerId();
        //3.判断这个商品他的商家在原来的购物车中是否存在
        CartVo c = checkSellerId(cartlist, sellerId);
        //4.如果不存在,我们只需要往原来的购物车中添加数据就行了
        if (c ==null){
            c = new CartVo();
            c.setSellerId(sellerId);
            c.setSellerName(item.getSeller());
            List<TbOrderItem> orderItemList = new ArrayList<>();
            TbOrderItem orderItem = new TbOrderItem();
            //给集合中的对象赋值
            orderItem.setItemId(itemId);
            orderItem.setGoodsId(item.getGoodsId());
            orderItem.setSellerId(item.getSellerId());
            orderItem.setNum(num);
            orderItem.setPrice(item.getPrice());
            orderItem.setPicPath(item.getImage());
            orderItem.setTitle(item.getTitle());
//            总价格 = 数量* 单价
            orderItem.setTotalFee(new BigDecimal(orderItem.getPrice().doubleValue() * num));
            //将商品信息添加到集合中
            orderItemList.add(orderItem);
            //将集合添加到购物车的类中
            c.setCartDetailList(orderItemList);
            //将购物车中的信息添加到集合中
            cartlist.add(c);
        }else {//5.如果存在?

            List<TbOrderItem> cartDetailList = c.getCartDetailList();
            TbOrderItem orderItem = checkItemId(cartDetailList,itemId);
            //6.虽然存在该商家,但是原来购物车中并没有该商品,我们只要从原来购物中拿出该商家的明细往里面追加一条
            if(orderItem == null){
                orderItem = new TbOrderItem();
                //给集合中的对象赋值
                orderItem.setItemId(itemId);
                orderItem.setGoodsId(item.getGoodsId());
                orderItem.setSellerId(item.getSellerId());
                orderItem.setNum(num);
                orderItem.setPrice(item.getPrice());
                orderItem.setPicPath(item.getImage());
                orderItem.setTitle(item.getTitle());
//            总价格 = 数量* 单价
                orderItem.setTotalFee(new BigDecimal(orderItem.getPrice().doubleValue() * num));
                cartDetailList.add(orderItem);
            }else {//7.即存在商家,又买过该商品,那么我们不需要追加,只需要改商品的数量跟总价
                orderItem.setNum(orderItem.getNum() + num);
                orderItem.setTotalFee(new BigDecimal(item.getPrice().doubleValue() * orderItem.getNum()));
                if (orderItem.getNum() <= 0){
                    //如果我们购物车减少的话,当该商品减少为0的时候那么我们就要将这个商品从购物车明细列表中移除
                    cartDetailList.remove(orderItem);
                }
                //如果明细全部移除完了之后,那么我们就要将商家的购物车移除掉
                if(cartDetailList.size() == 0){
                    cartlist.remove(c);
                }
            }
        }
        return cartlist;
    }
    //根据商家的id获取购物车的对象
    private CartVo checkSellerId(List<CartVo> cartlist,String sellerId){
        CartVo c = null;
        for (CartVo cartVo : cartlist) {
            if (cartVo.getSellerId().equals(sellerId)){
                return cartVo;
            }
        }
        return null;
    }
    //获取商品详情的id,判断是否存在
    private TbOrderItem checkItemId(List<TbOrderItem> cartDetailList, Long itemId){
        for (TbOrderItem tbOrderItem : cartDetailList) {
            if (tbOrderItem.getItemId().equals(itemId)){
                return tbOrderItem;
            }
        }
        return null;
    }

    @Override
    public void addCartListToRedis(Long id, List<CartVo> cartlist) {
        redisTemplate.boundHashOps("cartList").put(id,cartlist);
    }

    @Override
    public List<CartVo> getCartListFormRedis(Long id) {
        List<CartVo> cartList = (List<CartVo>) redisTemplate.boundHashOps("cartList").get(id);
        String s = JSONUtil.toJsonStr(cartList);
        if (StrUtil.isBlank(s)){
            s = "[]";
        }
//        将字符串转换成集合
        List<CartVo> cartVos = JSONUtil.toList(s, CartVo.class);
        return cartVos;

    }

    @Override
    public List<CartVo> getCookieAndRedis(List<CartVo> cartListCookie, List<CartVo> cartListRedis) {
        for (CartVo cartVo : cartListCookie) {
            for (TbOrderItem tbOrderItem : cartVo.getCartDetailList()) {
                cartListRedis = selectItemById(cartListRedis,tbOrderItem.getItemId(),tbOrderItem.getNum());
            }
        }
        return cartListRedis;
    }
}
